home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / introduction / example4.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  5KB  |  123 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Introduction                Tulevagen 22       */
  8. /* File:    Example4.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-09-24                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example contains a useful function which converts hard to */
  21. /* use BSTR (BCPL stings) into normal easy to use C strings. This */
  22. /* example is not directly runnable and must instead be linked    */
  23. /* together with some other program.                              */
  24.  
  25.  
  26.  
  27. /* Include the dos library definitions: */
  28. #include <dos/dosextens.h>
  29.  
  30. /* Now we include the necessary function prototype files: */
  31. #include <clib/dos_protos.h>       /* General dos functions...    */
  32. #include <stdio.h>                 /* Std functions [printf()]  */
  33. #include <stdlib.h>                /* Std functions [exit()]    */
  34.  
  35.  
  36.  
  37. /* Set name and version number: */
  38. UBYTE *version = "$VER: AmigaDOS/AmigaDOS/Example4 1.0";
  39.  
  40.  
  41.  
  42. /* Declare the function: */
  43. void BSTRtoC
  44. (
  45.   BSTR string_bstr,
  46.   UBYTE * string_c,
  47.   int length_c
  48. );
  49.  
  50.  
  51.  
  52. /* Converts a BCPL string (BSTR) into a normal C string: */
  53. void BSTRtoC
  54. (
  55.   BSTR string_bstr, /* The BSTR (BCPL string)         */
  56.   UBYTE *string_c,  /* Pointer to a normal C string   */
  57.   int length_c      /* Maximum length of the C string */
  58. )
  59. {
  60.   /* Temporary string pointer: */
  61.   UBYTE *string_ptr;
  62.  
  63.   /* The length of the BSTR string: (A BSTR can not be   */
  64.   /* longer than 255 characters so we can use a unsigned */
  65.   /* byte to store the length in.)                       */
  66.   UBYTE length_bstr;
  67.  
  68.   /* The number of characters that will be copied: */
  69.   int length;
  70.  
  71.   /* Simple loop variable: */
  72.   int loop;
  73.     
  74.   
  75.   /* Since we have to put a NULL sign at the end of the  */
  76.   /* C string we can only store "length_c" - 1 number of */
  77.   /* characters. Therefore we have to reduce the length  */
  78.   /* by one:                                             */
  79.   length_c--;
  80.   
  81.   /* Convert the BSTR into a normal C pointer */
  82.   /* to a BCPL string: (Are you with me?)     */
  83.   string_ptr = (UBYTE *) BADDR( string_bstr );
  84.   
  85.   /* Get the length of the BCPL string: (A BCPL string  */
  86.   /* does not contain a NULL sign at the end, but uses  */
  87.   /* instead the first byte to tell how many characters */
  88.   /* the string contains. A BCPL string (BSTR) can      */
  89.   /* therefore not contain more than 255 characters     */
  90.   /* (0 - 255 = one byte).                              */
  91.   length_bstr = string_ptr[ 0 ];
  92.   
  93.   /* Get the smallest value: (If the C string is smallest  */
  94.   /* we should of course not copy more than can be fitted  */
  95.   /* in the C string. On the other hand, if the BCPL       */
  96.   /* string is smaller we should of course not copy more   */
  97.   /* characters than there actually exist in the BCPL      */
  98.   /* string. Consequently we should only use the smallest  */
  99.   /* value: (If you have included the header file "math.h" */
  100.   /* you can equally well use the macro "min()".)          */
  101.   length = length_c <= length_bstr ? length_c : length_bstr;
  102.  
  103.   /* Convert the BCPL string into a C string: */
  104.   for( loop = 1; loop <= length; loop++ )
  105.     string_c[ loop - 1] = string_ptr[ loop ];
  106.  
  107.   /* Note that the loop starts with 1 and not 0 as normal! */
  108.   /* The first byte in a BCPL string contains the lenght   */
  109.   /* of the string, and since we don't want to copy that   */
  110.   /* value into the C string we start one byte later. We   */
  111.   /* must then also change the "<" sign into a "<=" since  */
  112.   /* we want to copy all characters including the last     */
  113.   /* one since that also contains a character. Normal C    */
  114.   /* strings ends with a NULL, but BSTRs do not.           */
  115.  
  116.  
  117.   /* Finally we have to put a NULL sign at the */
  118.   /* end of the C string:                      */
  119.   string_c[ loop - 1] = NULL;
  120. }
  121.  
  122.  
  123.